package cn.hg.solon.youcan.system.provider;

import static cn.hg.solon.youcan.system.entity.table.SysConfigTableDef.SYS_CONFIG;

import java.io.Serializable;
import java.util.Collection;
import java.util.Map;

import org.dromara.hutool.core.text.CharSequenceUtil;
import org.dromara.hutool.core.text.StrValidator;
import org.dromara.hutool.core.util.ObjUtil;
import org.noear.solon.annotation.Component;
import org.noear.solon.data.annotation.Tran;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryColumn;
import com.mybatisflex.core.query.QueryOrderBy;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.solon.service.impl.ServiceImpl;

import cn.hg.solon.youcan.common.constant.AppConstants;
import cn.hg.solon.youcan.common.exception.ServiceException;
import cn.hg.solon.youcan.system.entity.SysConfig;
import cn.hg.solon.youcan.system.mapper.SysConfigMapper;
import cn.hg.solon.youcan.system.service.SysConfigService;

/**
 * @author 胡高
 */
@Component
public class SysConfigProvider extends ServiceImpl<SysConfigMapper, SysConfig> implements SysConfigService {

    @Override
    public boolean checkUnique(SysConfig bean) {
        // 查找已存在记录
        QueryWrapper query = QueryWrapper.create()
            .where(SYS_CONFIG.TYPE.eq(bean.getType())
                .and(SYS_CONFIG.CODE.eq(bean.getCode()))
                .and(SYS_CONFIG.ID.ne(bean.getId()))
            );

        return ObjUtil.isNull(this.getOne(query));
    }

    /* (non-Javadoc)
     * @see cn.hg.solon.youcan.system.service.SysConfigService#getByTypeAndCode(java.lang.String, java.lang.String)
     */
    @Override
    public SysConfig getByTypeAndCode(String type, String code) {
        QueryWrapper query = QueryWrapper.create().where(SYS_CONFIG.TYPE.eq(type).and(SYS_CONFIG.CODE.eq(code)));
        return this.getOne(query);
    }

    @Override
    public Page<SysConfig> pageByMap(Page<SysConfig> page, Map<String, Object> paraMap) {
        String type = (String)paraMap.get("type");
        String word = (String)paraMap.get("word");
        String sortField = (String)paraMap.get("sortField");
        String sortType = (String)paraMap.get("sortType");
        QueryWrapper query = QueryWrapper.create()
            .where(SYS_CONFIG.TYPE.eq(type).when(StrValidator.isNotBlank(type))
                .and(SYS_CONFIG.NAME.like(word).when(StrValidator.isNotBlank(word))
                    .or(SYS_CONFIG.CODE.like(word).when(StrValidator.isNotBlank(word)))
                    .or(SYS_CONFIG.VALUE.like(word).when(StrValidator.isNotBlank(word)))
                )
            );

        if (StrValidator.isNotBlank(sortField) && StrValidator.isNotBlank(sortType)) {
            query.orderBy(new QueryOrderBy(new QueryColumn(CharSequenceUtil.toUnderlineCase(sortField)), sortType));
        }

        return this.page(page, query);
    }

    @Override
    @Tran
    public boolean removeByIds(Collection<? extends Serializable> ids) {
        for (Serializable id : ids) {
            SysConfig item = this.getById(id);

            if (ObjUtil.isNotNull(item) && item.getIsSys()) {
                throw new ServiceException(AppConstants.RETURN_CODE_VALUE_FAILED, "[" + item.getName() + "] 为系统参数，不允许删除！");
            }
        }

        return super.removeByIds(ids);
    }

    @Override
    public boolean save(SysConfig bean) {
        if (!this.checkUnique(bean)) {
            throw new ServiceException(AppConstants.RETURN_CODE_VALUE_FAILED, "参数代码已经存在，请更换其它值！");
        }

        return super.save(bean);
    }

    @Override
    public boolean update(SysConfig bean) {
        if (!this.checkUnique(bean)) {
            throw new ServiceException(AppConstants.RETURN_CODE_VALUE_FAILED, "参数代码已经存在，请更换其它值！");
        }

        return this.updateById(bean);
    }

}
